﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runnergame.View;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using runnergamexna.Model;

namespace Runnergame.Model
{
    enum PlayerState
    {
        onground,
        jumping,
        falling    
    }

    class Player
    {
        private const float PLAYER_STARTSPEED = 0.1f;
        private float time;
        public float playerSpeed;
        public float playerSize = 1f;
        public Vector2 playerVectorSize;
        public Vector2 playerPosition;
        public Vector2 velocity;
        public Vector2 gravity = new Vector2(0, 20f);
        public Texture2D runnerTexture;
        public PlayerState playerState;
        public Model.Collision collision;

        public Player(Level level)
        {
            playerSpeed = PLAYER_STARTSPEED;
            playerPosition = new Vector2(5, 6);            
            collision = new Collision(this, level);
            playerVectorSize = new Vector2(playerSize, playerSize);

        }

        internal void LoadContent(ContentManager content)
        {

            runnerTexture = content.Load<Texture2D>("temprunner1");

        }
        internal void Update(GameTime gameTime, Level level) 
        {
            Vector2 playerOldPosition = playerPosition;
            System.Diagnostics.Debug.WriteLine(playerPosition);
            System.Diagnostics.Debug.WriteLine(velocity);
            time = (float)gameTime.ElapsedGameTime.TotalSeconds;

            velocity += gravity * time;
            playerPosition.Y += velocity.Y * time;

            if (playerState == PlayerState.jumping || playerState == PlayerState.falling)
            {

            }

            playerPosition.X += velocity.X;

            Vector2 playerBottomCenter = new Vector2(playerPosition.X + playerVectorSize.X / 2, playerPosition.Y + playerVectorSize.Y);

            if (collision.didCollide(playerBottomCenter, playerVectorSize))
            {
                CollisionDetails details = collision.getCollisionDetails(playerOldPosition, playerPosition, playerVectorSize, velocity);
                if (details.m_hasCollidedWithGround)
                {
                    playerState = PlayerState.onground;  
                }


                //set the new speed and position after collision
                playerPosition = details.m_positionAfterCollision;
                playerPosition = new Vector2(playerPosition.X - playerVectorSize.X / 2, playerPosition.Y - playerVectorSize.Y);
                velocity = details.m_speedAfterCollision;
            }



        }

        internal void jump()
        {
            //jumping height
            velocity.Y = velocity.Y - 10f;
            playerState = PlayerState.jumping;

        }
        //gammal collision, ta bort efter implementaiton av daniels
        //internal Tile Collision(Level level)
        //{

        //    foreach (Tile tile in level.tileList)
        //    {

        //        if (tile.tileRect.Intersects(playerRect) && tile.tileCollision == TileCollision.Impassable)
        //        {
        //            System.Diagnostics.Debug.WriteLine("player bottom: {0}", playerRect.Bottom);
        //            System.Diagnostics.Debug.WriteLine("tile top:{0}", tile.getTileRect().Top);
        //            //standing on
        //             if (playerPosition.Y < tile.tilePosition.Y)
        //            {
        //                playerState = PlayerState.onground;
        //                playerPosition.Y = tile.tileRect.Top;
        //            }
        //            //jumping collision
        //            if (playerRect.Top < tile.getTileRect().Bottom)
        //            {
        //                playerState = PlayerState.falling;
        //                playerPosition.Y = tile.tilePosition.Y - 10f;
        //                velocity += gravity * time;
        //                //playerPosition += velocity * time;
        //            }
        //            if (playerRect.Right < tile.tileRect.Left)
        //            {
        //                playerPosition.X = tile.tilePosition.X - 1;
        //            }
        //            if (playerRect.Left > tile.tileRect.Right)
        //            {
        //                playerPosition.X = tile.tilePosition.X + 1;   
        //            }
        //        }
        //        //if(!playerRect.Intersects(tile.tileRect))
        //        //{
        //        //    playerState = PlayerState.falling;
        //        //}                
        //    }
        //    return null;
            
        
        //}

        internal void Draw(SpriteBatch spriteBatch, Model.Player player, Camera camera)
        {
            Vector2 position = camera.getVisualVector(playerPosition);
            Vector2 size = camera.getVisualVector(playerVectorSize);
            Rectangle playerVisualRect = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
            spriteBatch.Draw(runnerTexture, playerVisualRect, Color.White);

        }

        //internal bool IsCollidingAt(FloatRectangle a_rect)
        //{

        //    Vector2 tileSize = new Vector2(1, 1);
        //    for (int x = 0; x < g_levelWidth; x++)
        //    {
        //        for (int y = 0; y < g_levelHeight; y++)
        //        {
        //            FloatRectangle rect = FloatRectangle.createFromTopLeft(new Vector2(x, y), tileSize);
        //            if (a_rect.isIntersecting(rect))
        //            {
        //                if (m_tiles[x, y].isBlocked())
        //                {
        //                    return true;
        //                }
        //            }
        //        }
        //    }
        //    return false;
        //}
    }
}
